﻿using System.Collections;
using UnityEngine;
using UnityEngine.Video;

namespace VCity
{
    public class VideoTrigger : MonoBehaviour
    {
        public AdsBoard adsBoard;

        private void OnValidate()
        {
            if (adsBoard == null)
            {
                adsBoard = GetComponent<AdsBoard>();
                if (adsBoard == null && gameObject.transform.parent != null)
                    adsBoard = GetComponentInParent<AdsBoard>();
            }
        }

        private void OnTriggerEnter(Collider other)
        {
            if (IsPlayer(other))
            {
                adsBoard.PlayVideo(true);
            }
        }

        private void OnTriggerExit(Collider other)
        {
            if (IsPlayer(other))
            {
                adsBoard.PlayVideo(false);
            }
        }

        bool IsPlayer(Collider other)
        {
            return other.GetComponent<CharCtrl>() == ClientMain.Instance.charCtrl;
        }
    }
}